package com.xja.web.domain.controllers;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.xja.web.domain.commons.Constant;
import com.xja.web.domain.commons.SHA1;
import com.xja.web.domain.model.JsonBean;
import com.xja.web.domain.model.WxUser;
import com.xja.web.domain.service.WeChatService;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.applet.Main;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

@RestController
public class WechatController {
    @Autowired
    private WeChatService weChatService;

    //微信服务器与后台服务器的握手行为
    @GetMapping("index")
    public String getWechatMsg(String signature, String timestamp, String nonce, String echostr) {
        List<String> list = new ArrayList();
        list.add(timestamp);
        list.add(nonce);
        list.add("20200713A1905");
        Collections.sort(list);
        String str = "";
        for (String param : list) {
            str += param;
        }
        String encode = SHA1.encode(str);
        if (encode.equals(signature)) {
            return echostr;
        }
        return "";
    }

    @PostMapping("index")
    public String postWechatMsg(HttpServletRequest request) {
        Element rootElement = null;
        String s=null;
        try {
            ServletInputStream inputStream = request.getInputStream();
            rootElement = new SAXBuilder().build(inputStream).getRootElement();
            s= weChatService.uodateByUserMsg(rootElement);


        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }
    @PostMapping("jsConfig")
    public JsonBean postjsConfig(String url,String code,String type,String openid) {
              Map<String, Object> getconfig= Constant.getconfig(url);
        getconfig.put("openid",openid);
            if (!code.equals("")){
                WxUser userInfoByOP = Constant.getUserInfoByOP(code);
                getconfig.put("openid",userInfoByOP.getOpenid());
            }

        getconfig = weChatService.upDateCode(getconfig,type);
        System.out.println(getconfig.get("ticket").toString());
        return  new JsonBean(0, "成功", getconfig);
    }

    public static void main(String[] args) {
        System.out.println(11111);
    }



    @PostMapping("oAuth")
    public JsonBean oAuth(String code,HttpServletRequest request) {
        JsonBean jsonBean = new JsonBean(-1, "失败", null);
            WxUser wxUser = Constant.getUserInfoByOP(code);
            jsonBean = new JsonBean(0, "成功", wxUser.getOpenid());
            System.out.println(wxUser);
            request.getSession().setAttribute("wxUser",wxUser);
        return jsonBean;
    }
}